#!/bin/bash
port=$1
domain=$2
if [ $UID != 0 ]; then
	echo '非root账号,正在切换root账号,请输入root账号密码后继续运行'
	sudo su
	if [ $UID != 0 ]; then
		echo '切换root账号失败,请切换到root账号后重新尝试.'
		exit
	fi
fi
apt-get update
if ! [ -f "/bin/docker" ]; then
	echo "正在安装Docker"
	curl -fsSL https://get.docker.com | bash
	if ! [ -f "/bin/docker" ]; then
		apt-get install -y docker-io
	fi
fi
apt-get install -y openssh-client

function installOpenvpn()
{
	docker volume create --name $1
	docker run -v $1:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -t -u udp://${domain}:$2
	echo "密码必须超过4位"
	docker run -v $1:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki
	docker run --name=$1 \
		-v $1:/etc/openvpn \
		--restart=always \
		-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro \
		-d -p $2:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn
	docker run -v $1:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full temp-server nopass
	docker run -v $1:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient temp-server > $1.ovpn
}

installOpenvpn ovpn-data${port} ${port}
docker ps -a
ls -l *.ovpn
exit 0



